﻿using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using EasyComm.Base.Hosting;

namespace EasyComm.Base
{
    [DataContract]
    public class DynamicServiceUnitBase
    {
        [DataMember]
        public ServiceStatus Status { get; set; }

        [DataMember]
        public string Error { get; set; }

        [DataMember]
        public string Host { get; set; }

        [DataMember]
        public int Port { get; set; }

        [DataMember]
        public virtual ServiceContractCollection Contracts { get; private set; }

        [DataMember]
        public int ProcessID { get; private set; }

        [DataMember]
        public Uri ListenUri { get; set; }

        public DynamicServiceUnitBase(EasyCommServiceHostBase serviceHost)
        {
            if (serviceHost != null)
            {
                Contracts = serviceHost.Contracts;
                if (Contracts.Count == 1)
                {
                    ListenUri = Contracts[0].ListenUri;
                }
                Host = serviceHost.BaseAddresses[0].DnsSafeHost;
                Port = serviceHost.BaseAddresses[0].Port;
            }
            ProcessID = Process.GetCurrentProcess().Id;
        }
    }
}
